<?php
namespace Home\Controller;
use Think\Controller;

class UpdateController extends CommonController {
	
    public function index(){
    	
    	if(IS_POST){
	
        }else{
        	$picture = M('Picture')->where(array('xuehao'=>session('xuehao'),'status'=>1))->select();
        	dump($picture);
        	$this->assign('picture',$picture);
			$this->meta_title = "照片上传";
        	$this->display(); 
        }
    }

	public function img(){ 
		set_time_limit(1800);
		if (!empty($_FILES)) {  
			//dump(I());
			//dump($_FILES);
			//图片上传设置
			$config = array(   
				'maxSize'    =>    1024*1024*4, 
				'rootPath'      =>  './', //保存根路径
				'savePath'   =>    '/Uploads/Picture/',
				'saveName'   =>    session('xuehao'), 
				'exts'       =>    array('gif', 'jpg', 'jpeg', 'bmp','png'),  
				'replace'    =>    true,   
				'subName'    =>    array('date','Ymd'),
			);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info = $upload->upload();
			//dump($info);
			//exit;
			//判断是否有图
			if($info){
				
				$model = M('File');// 取得成功上传的文件信息

				//dump($info);
				$data = $info['file'];
				$data['mime'] = $info['file']['type'];
				$data['create_time'] = NOW_TIME;

				$data['uid'] = I('uid') ;
				
				//log2($data['filename']);
				//dump($data);
				$result = $model->add($data);
				
				$picre = M('Picture')->where('xuehao='.I('uid'))->select();
				$picdata = array(
					'xuehao'=>session('xuehao'),
					'fid'=>$result,
					'shear'=>'',
					'path'=>$data['savepath'].$data['savename'],
					'name'=>$data['name'],
					'size'=>$data['size'],
					'md5'=>$data['md5'],
					'sha1'=>$data['sha1'],
					'status'=>1,
					//'create_time'=>time(),
					'last_time'=>time(),
				);
				if (empty($picre)) {
					$picdata['create_time'] = time();
					$pic = M('Picture')->add($picdata);
				}else{
					$pic = M('Picture')->where('xuehao='.I('uid'))->save($picdata);
				}
				

				if($result){
					// 如果主键是自动增长型 成功后返回值就是最新插入的值
					$durl = $data['savepath'].$data['savename'];
					$info = array("status"=>1,
								"msg" => "上传成功",
								"url" => $durl, 
								'fileid' => $result,
							
							);
					//exit(json_encode($info));
					
					//log2($info['swfurl']);
					//log2($info['pdfurl']);
					//$this->success($info);
					$this->ajaxReturn($info);
				}else{
					$info = array("status"=>0,"msg" => $model->getError(),"url" => '', 'id' => $result );
					$this->ajaxReturn($info);
				}

			}else{
				$info = array("status"=>0,"msg" => $upload->getError(),"url" => '', 'id' => $result );
				$this->ajaxReturn($info);
			}
		}else{
			$this->error("请求错误");
		}

	}

	public function edit_img(){

		$picture = M('Picture')->where(array('xuehao'=>session('xuehao'),'status'=>1))->find();
		if(IS_POST){
			$image = new \Think\Image(); 
			$image->open('.'.$picture['path']);//将图片裁剪为400x400并保存为corp.jpg
			//$image->crop(400, 400)->save('./Uploads/Share/1'.$picture['xuehao'].'.jpg');

			$width = $image->width(); // 返回图片的宽度
			if ($width > 600) {

				$l = $width / 600;
				$width = 600;
				$height =  $image->height() / $l; 
			}


			$image->thumb($width , $height)->save('./Uploads/thumb/'.$picture['xuehao'].'.jpg');

			$image->open('./Uploads/thumb/'.$picture['xuehao'].'.jpg');//将图片裁剪为400x400并保存为corp.jpg
			$image->crop(I('w'), I('h'),I('x'),I('y'))->save('./Uploads/Share/'.$picture['xuehao'].'.jpg');
			
			 M('Picture')->where(array('xuehao'=>session('xuehao')))->save(array('shear'=>'./Uploads/Share/'.$picture['xuehao'].'.jpg'));
			$this->success('裁切成功，进入报名页面。');
        }else{

		
			$this->assign('picture',$picture);
	    	$this->display();
	    }
    }
}